$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            this.form = new xhForm($('#xh_form'));
            this.form.parse();
            if (info) {
                this.form.fill(info);
            }
            $('[name=depId]').select2();
            this.listener();
        },
        listener:function () {
            var $this = this;

            //录用信息保存
            $('#save').click(function () {
                $this.funObj().submitForm()
            });

            $('#employ').click(function () {
                $this.funObj().employAction();
            })

        },
        funObj:function () {
            var $this = this;
            return {
                submitForm:function () {
                    $this.form.submit('hrm/resource/saveSource',{},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        location.reload();
                    })
                },
                addForm:function (sTag) {
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '审批意见',
                        input: 'textarea',
                        preConfirm: function(explain) {
                            return new Promise(function (resolve, reject) {
                                if(!explain){
                                    reject('说明不能为空！');
                                }
                                else {
                                    resolve(explain);
                                }
                            })
                        }
                    }).then(function(explain) {
                        $this.funObj().saveExplain(sTag,explain);
                    }, function(dismiss) {});
                },
                editForm:function (iRAId, mDesc) {
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '审批意见',
                        inputValue:mDesc,
                        input: 'textarea',
                        preConfirm: function(explain) {
                            return new Promise(function (resolve, reject) {
                                if(!explain){
                                    reject('说明不能为空！');
                                }
                                else {
                                    resolve(explain);
                                }
                            })
                        }
                    }).then(function(explain) {
                        $this.funObj().saveExplain('',explain,iRAId);
                    }, function(dismiss) {});
                },
                saveExplain:function (sTag,explain,iRAId) {
                    var obj = {};
                    obj.sType = 'employ';
                    if (sTag) {
                        obj.sVerifier = sTag;
                    }
                    obj.mDesc = explain;
                    if (iRAId) {
                        obj.iRAId = iRAId;
                    }
                    obj.iTargetId = sourceId;
                    xhExtra.ajaxRequest('hrm/resource/saveApprove',obj,{},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        location.reload();
                    })
                },
                employAction:function () {
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '发起入职',
                        html:'<input id="date" class="form-control" type="date" placeholder="选择日期"><br>' +
                        '<textarea id="desc" class="form-control" rows="3"></textarea>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var date = $('#date',window.top.document).val();
                                if(!date){
                                    reject('日期不能！');
                                }
                                else {
                                    resolve(date);
                                }
                            })
                        }
                    }).then(function(date) {
                        var desc = $('#desc',window.top.document).val();
                        xhExtra.ajaxRequest('hrm/resource/saveEntry',{id:info['iStaffId'],time:date,desc:desc},{},function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                        })
                    }, function(dismiss) {});
                }
            }
        }
    }
};